package com.yixing.tech.wmsjob.service.impl;

import com.yixing.tech.common.base.BusinessException;
import com.yixing.tech.wcs.constant.DictId;
import com.yixing.tech.wcs.domain.po.SubTask;
import com.yixing.tech.wcs.domain.po.Task;
import com.yixing.tech.wms.service.*;
import com.yixing.tech.wms.stock.out.callback.WCSCallbackService;
import com.yixing.tech.common.entity.WCSTaskCallbackVO;
import com.yixing.tech.wcs.service.ICRUDService;
import com.yixing.tech.wcs.service.TaskNotifyService;
import com.yixing.tech.wms.stock.out.callback.IMovingTaskCallbackService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


@Slf4j
@Service
public class TaskNotifyServiceImpl implements TaskNotifyService {

    @Autowired
    private IMovingTaskCallbackService movingTaskCallbackService;
    @Autowired
    private ICRUDService crudService;
    @Autowired
    private InitService initService;
    @Autowired
    private InitClearService initClearService;
    @Autowired
    private WCSCallbackService wcsCallbackService;
    @Autowired
    private InventoryService inventoryService;
    @Autowired
    private LineOperationService lineOperationService;
    @Autowired
    private WcsWmsCallbackService wcsWmsCallbackService;
    @Autowired
    private ManualInOutStockService manualInOutStockService;


    @Override
//    @Async
    public void notifyTaskExecResult(String subTaskCode, String action) throws BusinessException {
        SubTask subTask = crudService.querySubTaskByTaskCode(subTaskCode);
        if(ObjectUtils.isEmpty(subTask))
            return;

        Task task = crudService.queryTaskByTaskCode(subTask.getParentTask());
        if(ObjectUtils.isEmpty(task))
            return;

        WCSTaskCallbackVO callbackVO = new WCSTaskCallbackVO();
        callbackVO.setTaskCode(task.getTaskCode());
        callbackVO.setPodCode(task.getPodCode());
        callbackVO.setMethod("outbin");
        callbackVO.setCurrentPositionCode(task.getStartLoc());

        String business = task.getBusiness();
        callbackWmsBiz(business, callbackVO);
        log.info("通知上层系统任务离开起点:taskCode[{}],req[{}]", task.getTaskCode(), callbackVO);


        callbackVO.setMethod("finish");
        callbackVO.setCurrentPositionCode(task.getEndLoc());
        callbackWmsBiz(business, callbackVO);
        log.info("通知上层系统任务到达终点:taskCode[{}],req[{}]]", task.getTaskCode(), callbackVO);
    }

    /**
     * 根据不同的业务类型回调WMS上层业务系统
     * WCS（仓库控制系统）完成任务后，将任务执行状态回调给WMS（仓库管理系统）的不同业务模块
     *
     * @param business 业务类型标识，用于区分不同的业务场景
     * @param callbackVO WCS任务回调数据对象，包含任务执行状态等信息
     */
    private void callbackWmsBiz(String business, WCSTaskCallbackVO callbackVO) {
        switch (business) {
            case "初始化入库":           // WCS初始化入库任务状态回调处理
                initService.handleCallback(callbackVO);
                break;
            case "初始化清除下架":       // WCS初始化清除下架任务状态回调处理
                initClearService.handleCallback(callbackVO);
                break;
            // ctuCallback
            case "stockOut2":          // CTU出库任务状态回调处理
                wcsCallbackService.handleCtuCallback(callbackVO);
                break;
            case "salesPackOutReturn": // 销售包装出库退货任务状态回调处理
                wcsCallbackService.handleSalesPackOutStockCallback(callbackVO);
                break;
            case "盘点":               // 盘点任务状态回调处理
                inventoryService.callback(callbackVO);
                break;
            case "matDelivery":        // 物料配送任务状态回调处理
                wcsCallbackService.handleMatDeliveryCallback(callbackVO);
                break;
            case "otherOutMatReturn":  // 其他出库物料退货任务状态回调处理
                wcsCallbackService.handleOtherOutCallback(callbackVO);
                break;
            case "stockOut":           // 普通出库任务状态回调处理
                wcsCallbackService.handleCallback(callbackVO);
                break;
            case "产线呼叫空框":       // 产线呼叫空框任务状态回调处理
                lineOperationService.productCallEmptyCallback(callbackVO);
                break;
            case "入库":               // 入库任务状态回调处理
                wcsWmsCallbackService.handleCallback(callbackVO);
                break;
            case "手动出入库":         // 手动出入库任务状态回调处理
                manualInOutStockService.handleCallback(callbackVO);
                break;
        }
    }

    /** 通知上层业务服务发生了移库动作 */
    @Override
    public void notifyMovingTaskAction(String subTaskCode, String action) throws BusinessException {
        if("finish".equals(action)) {
            SubTask subTask = crudService.querySubTaskByTaskCode(subTaskCode);
            if(ObjectUtils.isNotEmpty(subTask) && DictId.TaskType.MOVING.equals(subTask.getTaskType())) {

                Task task = crudService.queryTaskByTaskCode(subTask.getParentTask());
                if(ObjectUtils.isNotEmpty(task)) {
                    if(!subTask.getStartLoc().equals(task.getStartLoc()) && !subTask.getEndLoc().equals(task.getEndLoc())) {
                        log.info("子任务为正常出入库中产生的移库任务，上报给业务服务:subTaskCode[{}],taskCode[{}]", subTask.getTaskCode(), task.getTaskCode());
                        movingTaskCallbackService.handle(subTask.getStartLoc(), subTask.getEndLoc(), subTask.getPodCode());
                    }
                }
            }
        }
    }

}
